package com.zxs.test.controller;

import java.util.*;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.YamlProcessor.ResolutionMethod;
import org.springframework.core.env.JOptCommandLinePropertySource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zxs.test.dev.model.gen.Adminuser;
import com.zxs.test.service.UserService;

@RestController
public class UserController {
	@Autowired
	private UserService userService;
	@RequestMapping(value="/user",method=RequestMethod.GET)
	public List<Adminuser> getAllUser(){
		List<Adminuser> list = this.userService.getAllUser();
		return list;
	}
	@RequestMapping(value="/updateuser",method=RequestMethod.POST)
	@ResponseBody
	public int updateUser(@RequestBody String params) {
		JSONObject jsonObject=JSON.parseObject(params);
		Adminuser adminuser = new Adminuser();
		adminuser.setId(Integer.parseInt(jsonObject.getString("id")));
		adminuser.setUserid(jsonObject.getString("userid"));
		adminuser.setPwd(jsonObject.getString("pwd"));
		adminuser.setText(jsonObject.getString("text"));
		adminuser.setComments(jsonObject.getString("comments"));
		int tmp=this.userService.updateUser(adminuser);
		return tmp;
	}
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public int insertUser(@RequestBody String params) {
		JSONObject jsonObject=JSON.parseObject(params);
		Adminuser adminuser = new Adminuser();
		adminuser.setUserid(jsonObject.getString("userid"));
		adminuser.setPwd(jsonObject.getString("pwd"));
		adminuser.setText(jsonObject.getString("text"));
		adminuser.setComments(jsonObject.getString("comments"));
		int tmp= this.userService.insertUser(adminuser);
		System.out.println("id:"+adminuser.getId());
		System.out.println("userid:"+adminuser.getUserid());
		return tmp;
	}
	@RequestMapping(value="/deleteuser",method=RequestMethod.POST)
	public String deleteUser(@RequestParam("id") int id) {
		int tmp = this.userService.deleteUser(id);
		if(tmp==-1) {
			System.out.println("输入的id值小于1");
			return "输入的id值小于1";
		}else {
			return "删除成功";
		}
	}
	@RequestMapping(value="/deleteuserbyexample",method=RequestMethod.POST)
	public String deleteUseByExample() {
		int tmp=this.userService.deleteUserByExample();
		if(tmp<1) {
			return "删除失败！";
		}
		else {
			return "成功删除所有ID大于18的数据表";
		}
	}
}
